package com.mars.diancan.controller;

import com.mars.diancan.common.ResultEnum;
import com.mars.diancan.config.ProjectUrlConfig;
import com.mars.diancan.constant.CookieConstant;
import com.mars.diancan.constant.RedisConstant;
import com.mars.diancan.pojo.SellerInfo;
import com.mars.diancan.service.SellerService;
import com.mars.diancan.utils.CookieUtil;
import com.mars.diancan.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 卖家用户登录/登出操作
 * @author MARS
 * @date 2018/7/9
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {
    @Autowired
    private SellerService sellerService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    /**
     * 用户登录(分布式)
     */
    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,HttpServletResponse response,
                      Map<String,Object> map) {
        // 1、将openid 和数据库中的openid 比较
        SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid);
        // 如果没根据openid获取到数据，返回错误界面
        if (sellerInfo == null){
            map.put("msg",ResultEnum.LOGIN_FAIL.getMsg());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }

        // 2、设置token到redis
        //  - 获取token
        String token = UUID.randomUUID().toString();
        // - 获取过期时间
        Integer expire = RedisConstant.EXPIRE;
        // - 设置到redis(key,value,expireTime,time单位)
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);

        // 3、设置到cookie中
        CookieUtil.set(response, CookieConstant.TOKEN,token,expire);
        return new ModelAndView("redirect:"+ projectUrlConfig.getSell()+"/sell/seller/order/list");

    }

    /**
     * 用户退出
     * @param request
     * @param response
     * @param map
     * @return
     */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                               HttpServletResponse response,
                               Map<String ,Object> map){
        // 1、从cookie中查询
        Cookie cookie = CookieUtil.get(request,CookieConstant.TOKEN);


        if (cookie != null ) {
            // 2、清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue() ));
            // 3、清除cookie,将cookie值设置为0 ，就是过期
            CookieUtil.set(response,CookieConstant.TOKEN,null,0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMsg());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }
}
